package yl.hs.bmipfull.service.production.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.production.IFinishProductInventoryDao;
import yl.hs.bmipfull.daos.production.IFinishProductOutboundRecordDao;
import yl.hs.bmipfull.daos.production.IFinishProductOutboundRecordDetailDao;
import yl.hs.bmipfull.pojo.production.FinishProductInventoryEntity;
import yl.hs.bmipfull.pojo.production.FinishProductOutboundRecordDetailEntity;
import yl.hs.bmipfull.pojo.production.FinishProductOutboundRecordEntity;
import yl.hs.bmipfull.pojo.production.FinishProductWarehouseRecordEntity;
import yl.hs.bmipfull.pojo.salemanage.SaleOrderProductDetailEntity;
import yl.hs.bmipfull.service.production.IFinishProductOutboundRecordService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * @author 谢东
 * @apiNote 成品出库记录-主表服务层接口实现
 * @since 2023年06月14日 15时45分46秒 <br/>
 */
@Service
public class FinishProductOutboundRecordServiceImpl extends ProductionBaseService implements IFinishProductOutboundRecordService {

    @Autowired
    IFinishProductOutboundRecordDao dao;
    @Autowired
    IFinishProductOutboundRecordDetailDao recordDetailDao;
    @Autowired
    IFinishProductInventoryDao inventoryDao;

    @Override
    public List<FinishProductOutboundRecordEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(FinishProductOutboundRecordEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<FinishProductOutboundRecordEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(FinishProductOutboundRecordEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<FinishProductOutboundRecordEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public FinishProductOutboundRecordEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    private void updateInventory(FinishProductOutboundRecordDetailEntity recordDetailEntity){
        if(recordDetailEntity !=null){
            var productInventory=inventoryDao.findByWareHouseSNAndProductSN(recordDetailEntity.getWareHouseSN(),recordDetailEntity.getProductSN(),recordDetailEntity.getPackageName(),recordDetailEntity.getPackageSn());
            if(productInventory!=null){
                //修改成品库存数量
                productInventory.setQuantity(productInventory.getQuantity().subtract(recordDetailEntity.getQuantity()));
                productInventory.setWeight(productInventory.getWeight().subtract(recordDetailEntity.getWeight()));
                inventoryDao.update(productInventory);
            }
        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveFinishProductOutboundRecord(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        FinishProductOutboundRecordEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new FinishProductOutboundRecordEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(FinishProductOutboundRecordEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入成品出库记录-主表信息", Tl.toJson(entity), FinishProductOutboundRecordEntity.class);
                List<Map<String, Object>> recordDetailList=(List<Map<String, Object>>)model.get("detail");
                insertDetail(recordDetailList,entity);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新成品出库记录-主表信息", Tl.toJson(entity), FinishProductOutboundRecordEntity.class);
            }
        }

        return result;
    }

    private void insertDetail(List<Map<String, Object>> recordDetailList,FinishProductOutboundRecordEntity recordEntity){
        recordDetailList.forEach(s->{
            var recordDetail = new FinishProductOutboundRecordDetailEntity();
            try {
                Tl.getChangeLog(recordDetail,s);
                recordDetail.setSn(queryNewSN(FinishProductOutboundRecordDetailEntity.class));
                recordDetail.setRecordSN(recordEntity.getSn());
                int result =recordDetailDao.insertNew(recordDetail);
                if (result > 0) {
                    writeLog(ActionType.CREATE, recordDetail.getSn(), "新建成品出库记录明细-子表信息", Tl.toJson(recordDetail), FinishProductOutboundRecordDetailEntity.class);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, FinishProductOutboundRecordEntity.class);
        }
        return result;
    }
}